<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\OutStockModel;
use think\Db;

class OutStock extends Common
{
    /**
     * 出库单 待出库 已出库 列表
     * @return string|\think\response\Json
     */
    public function index(Request $request, OutStockModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time'])?strtotime($data['start_time']):"";
        $data['end_time'] =isset($data['end_time'])?strtotime($data['end_time']):"";
        $data['number'] =isset($data['number'])?$data['number']:"";
        $list = $model->getOutList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 出库单 详情
     * @param $id 出库单id
     * @return \think\response\Json
     */
    public function detail($id, OutStockModel $model)
    {
        $list = $model->getInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function depot($id,Request $request,OutStockModel $model)
    {
        try {
            $option =$request->param('goods');
            $list = $model->getDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 出库单 撤销
     * @param $id 出库单id
     * @return \think\response\Json
     */
    public function revoke($id, OutStockModel $model)
    {
        $list = $model->getRevoke($id);
        if ($list ==1) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
}